﻿Imports System.IO

Public Class Form1
    Dim DumpLocation As String
    Dim AppLocation As String
    Dim filetoSend As String
    Sub SaveSettings()
        My.Settings.ArchiveDir = txtarchivedir.Text
        My.Settings.Rar = txtrar.Text
        My.Settings.Bol_Archive = chkMoveBack.Checked
        My.Settings.DeleteBackUpAfter = nudDelete.Value
        My.Settings.MoveBackUpAfter = nudMoveArchiveAfter.Value
        My.Settings.Save()

        MsgBox("Saved!", MsgBoxStyle.Information)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        My.Settings.Reload()
        txtarchivedir.Text = My.Settings.ArchiveDir
        txtrar.Text = My.Settings.Rar
        chkMoveBack.Checked = My.Settings.Bol_Archive
        nudDelete.Value = My.Settings.DeleteBackUpAfter
        nudMoveArchiveAfter.Value = My.Settings.MoveBackUpAfter

        'MsgBox(My.Settings.Rar)

        Try
            AppLocation = My.Application.Info.DirectoryPath
            Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
            For Each item In CommandLineArgs
                If item = "Automate" Then
                    BackupZip()
                    DeleteMoreThanDays()
                    MoveMoreThanDays()
                    Exit For
                End If
                DumpLocation = item
                Exit For
            Next
        Catch ex As Exception

        End Try



        'BackupZip()
        'DeleteMoreThan15Days()


        '\\192.168.1.204\NavisionBackup

        'MsgBox("load")
        'End
    End Sub
   
    Sub BackupZip()
        Dim Today As String
        Dim Yesterday As String
        If Now.Hour <= 10 Then
            Today = Now.AddDays(-1).ToString("yyyyMMdd")
            Yesterday = Now.AddDays(-2).ToString("yyyyMMdd")
        Else
            Today = Now.ToString("yyyyMMdd")
            Yesterday = Now.AddDays(-1).ToString("yyyyMMdd")
        End If
        'MsgBox(Today)


        Dim filename As String = "NAV" & Today & ".bak"
        'MsgBox(filename)
        Try
            Process1.StartInfo.FileName = My.Resources.Rar
            Process1.StartInfo.Arguments = " a -ep -df " & AppLocation & "\" & filename & ".rar " & AppLocation & "\" & filename & " -v100m"
            Process1.StartInfo.WindowStyle = ProcessWindowStyle.Normal
            Process1.StartInfo.UseShellExecute = False
            Process1.Start()
        Catch ex As Exception

        End Try
        'File = AppLocation & "\" & filename & ".rar

        
    End Sub

   
    
    Sub WriteLogs(Message As String)
        TextBox1.AppendText(Now & " : " & Message & vbCrLf)
        'SaveLogs(Now & " : " & Message)
    End Sub

    Private Sub DeleteMoreThanDays()
        Dim FileList As String()
        FileList = Directory.GetFiles(AppLocation, "NAV*bak*rar")
        For Each item In FileList
            Dim info As New FileInfo(item)
            If info.CreationTime <= Today.AddDays(-My.Settings.DeleteBackUpAfter) Then
                File.Delete(info.FullName)
            End If
        Next

    End Sub

    Function MoveMoreThanDays() As Boolean
        Dim FileList As String()
        FileList = Directory.GetFiles(AppLocation, "NAV*bak*rar")

        For Each item In FileList
            Dim info As New FileInfo(item)
            If info.CreationTime.Date = Today.AddDays(-My.Settings.MoveBackUpAfter).Date Then
                File.Copy(info.FullName, My.Settings.ArchiveDir & info.Name)
            End If
        Next
        Return False
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        BackupZip()
        DeleteMoreThanDays()
        MoveMoreThanDays()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SaveSettings()
    End Sub
End Class
